package com.scut.edu.wanyou.home;

import java.util.*;

import android.content.Context;
import android.widget.*;
import com.scut.edu.wanyou.home.talk.XmppTool;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.packet.Presence;

import com.scut.edu.wanyou.R;
import com.scut.edu.wanyou.TempValue;
import com.scut.edu.wanyou.home.talk.ChatOne;
import com.scut.edu.wanyou.home.talk.Friends;
import com.scut.edu.wanyou.home.talk.LoginAndReg;
import com.scut.edu.wanyou.widget.FlowIndicator;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;

/**
 * Description:
 * HomeActivity.java Create on 2013-1-7 上午10:47:56 
 * @author SongXitong * @version 1.0
 * Copyright (c) 2013 Company,Inc. All Rights Reserved.
 */
public class HomeActivity extends Activity {
    static final int SCROLL_ACTION = 0;
    ListView listView;
    int[] tags = new int[] { 0, 0 };
    public String[] groups = new String[] { "最近聊天", "我的好友"};
    public ArrayList<ChildHolder> friendList = new ArrayList();

    Gallery mGallery;
    GalleryAdapter mGalleryAdapter;
    FlowIndicator mMyView;
    Timer mTimer;
    public Friends friends;
    public MyListAdapter adapter;
    Intent chatIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.home_activity);

        friends = new Friends();
        friendList = friends.getFriends();

        prepareIntent();
        prepareView();

        /**
         * 绑定消息显示监听
         */
        for(int i = 0;i <= friendList.size() -1;i ++)
        {
            friendList.get(i).chat.initHomeListener(adapter, friendList.get(i));
        }

        mTimer = new Timer();
        mTimer.scheduleAtFixedRate(new MyTask(), 0, 5000);
    }

    private void prepareView() {
        listView = (ListView) findViewById(R.id.ListView1);
        adapter = new MyListAdapter(HomeActivity.this);
        View header = LayoutInflater.from(this).inflate(R.layout.header_view,null);
        mGallery = (Gallery) header.findViewById(R.id.home_gallery);
        mMyView = (FlowIndicator) header.findViewById(R.id.myView);
        mGalleryAdapter = new GalleryAdapter(this);
        mMyView.setCount(mGalleryAdapter.getCount());
        mGallery.setAdapter(mGalleryAdapter);
        mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                                       int arg2, long arg3) {
                // TODO Auto-generated method stub
                mMyView.setSeletion(arg2);
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
        listView.addHeaderView(header);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //To change body of implemented methods use File | Settings | File Templates.
                Intent intent = new Intent();
                TempValue.VALUES.put("ToHolder",friendList.get(i-1));
                friendList.get(i-1).msgNumUnRead = 0;
                adapter.notifyDataSetChanged();
                intent.setClass(HomeActivity.this,ChatActivity.class);
                startActivity(intent);

            }
        });
    }

    private class MyTask extends TimerTask {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(SCROLL_ACTION);
        }
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {
                case SCROLL_ACTION:
                    // int curPos = mGallery.getSelectedItemPosition();
                    // if (curPos < mGalleryAdapter.getCount() - 1) {
                    // curPos++;
                    // } else {
                    // curPos = 0;
                    // }
                    // // mGallery.setLayoutAnimation(new LayoutAnimationController(
                    // // AnimationUtils.loadAnimation(HomeActivity.this,
                    // // R.anim.gallery_in)));
                    // mGallery.setSelection(curPos, true);
                    MotionEvent e1 = MotionEvent.obtain(SystemClock.uptimeMillis(),
                            SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
                            89.333336f, 265.33334f, 0);
                    MotionEvent e2 = MotionEvent.obtain(SystemClock.uptimeMillis(),
                            SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
                            300.0f, 238.00003f, 0);

                    mGallery.onFling(e1, e2, -1300, 0);
                    break;

                default:
                    break;
            }
        }
    };

    public class MyListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MyListAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_child_item, null);
            }
            if(friendList.get(position).msgNumUnRead != 0)
                ((TextView)convertView.findViewById(R.id.textView2)).setText(""+friendList.get(position).msgNumUnRead);
            ((TextView) convertView.findViewById(R.id.textView1)).setText(friendList.get(position).Id);
            ((TextView) convertView.findViewById(R.id.textView3)).setText(friendList.get(position).stuts);
            ((ImageView) convertView.findViewById(R.id.imageView1)).setImageBitmap(friendList.get(position).userHead);
            return convertView;
        }

        @Override
        public int getCount() {
            return friendList.size();
        }

        @Override
        public Object getItem(int i) {

            return null;  //To change body of implemented methods use File | Settings | File Templates.
        }

        @Override
        public long getItemId(int i) {
            return 0;  //To change body of implemented methods use File | Settings | File Templates.
        }

        public boolean hasStableIds() {
            // TODO Auto-generated method stub
            return true;
        }

    }
    private void prepareIntent()
    {
        chatIntent = new Intent(this,ChatActivity.class);
    }



    public void onNewIntent()
    {
        Log.i("Home","重新进入");
    }

}
